package com.platform.modules.wallet.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.platform.common.exception.BaseException;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class BankCardUtil extends AppCode {

    public static JSONObject getBankInfo(String cardNo) {
        String host = "https://bkaear.market.alicloudapi.com";
        String path = "/bankcard/query";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("card", cardNo);


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            System.out.println("银行卡号：" + cardNo);
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            String str = EntityUtils.toString(response.getEntity());
            System.out.println("身份验证信息返回：" + str);

            JSONObject jsonObject = JSONUtil.parseObj(str);
            if (jsonObject.get("error_code").equals(0)) {
                return jsonObject.getJSONObject("result");
            } else {
                throw new BaseException("发生异常:" + jsonObject.getStr("reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("发生异常:银行卡出错");
        }
    }
}
